<?php

    if (!defined('EXPONENT')) exit('');

    global $db;
    if (!defined('SYS_SORTING')) require_once(BASE.'subsystems/sorting.php');
    
    $config = $db->selectObject('linklistmodule_config',"location_data='".serialize($loc)."'");
    if ($config == null) {
        $config->orderby = 'name';
        $config->orderhow = 0; // Ascending
        $config->force_title="";
        $config->item_per_page=20;
    }
    
    $max_item =  $config->item_per_page;
    
    $count_post = $db->countObjects('linklist_link',"location_data='".serialize($loc)."'");
    // page split
    // calculate how many posts
    $page_count=floor($count_post / $max_item);
    if ($count_post % $max_item != 0)
        $page_count++;

    // get page
    $page=1;
    if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
    if (($page<1) || ($page>$page_count)) $page=1;
    $start_pos=($page-1)*$max_item;
    
    $sortby = "";

    switch ($config->orderhow) {
        case 0:
            $sortby = " ORDER BY name ASC ";
            break;
        case 1:
            $sortby = " ORDER BY name DESC ";
            break;
        case 2:
             $sortby = " ORDER BY RAND() ";
            break;
        case 3:
            //sort the listings by their rank
             $sortby = " ORDER BY rank ASC ";
            break;
        case 4:
            //sort the listings by their rank
            $sortby = " ORDER BY rank DESC ";
            break;
    }
    
    $links = $db->selectObjects('linklist_link',"location_data='".serialize($loc)."' {$sortby}  LIMIT {$start_pos},{$max_item}");
    
    foreach ($links as $key=>$value){
        // check file id
        if ($value->file_id <= 0) {
            $value->picpath = '';
        } else {
            $file = $db->selectObject('file', 'id='.$value->file_id);
            $value->picpath = "/".$file->directory.'/'.$file->filename;
            $links[$key]=$value;
        }
        // Feb 11, 2011 - trungdt - add support feature tabs and banner tab - begin
        //$config->remove_body_html=1;
        // Remove html when confiugred
        if ($config->remove_body_html){
            //$value->description=htmlentities($value->description, ENT_QUOTES);
            $value->description=_ab_safe_html_string($value->description);
        }
        else
        {
            $value->description=str_replace("<p","<span",$value->description);
            $value->description=str_replace("</p","</span",$value->description);
        }
        // Feb 11, 2011 - trungdt - add support feature tabs and banner tab - end

        // June 4, trungdt - Add support SEO link
        // URL type : /xx_abc_def
        // which is: xx : linklist_link id
        $value->seo_link = $value->id."_"._to_vietnamese_with_nomark($value->name);
        // June 18, 2012 add date
        //$value->date_text  = strftime(DISPLAY_DATE_FORMAT, $value->post_date);
        $value->date_text = _ab_dateFormat($value->post_date, true, false);
    }

    $template = new template('linklistmodule','_Kurtulus_LinkTabs_OurTeam_Detail',$loc);
    $template->assign('links',$links);
    $template->assign('config',$config);
    $template->assign('team',1);
    $template->assign('pages',$page_count);
    $template->assign('page',$page);
    
    $template->register_permissions(
    array('administrate','configure','create','edit','delete'),$loc);

    $template->output();
    exponent_sessions_set("AB_THEME_RIGHT_MENU_SRC",$loc->src);
    
    exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
    // for the top container, we try to force them module from SRC we decided
    exponent_sessions_set("AB_THEME_TOP_CONTAINER_SRC","top_".$loc->src."_".LANG);
?>